% Forecasts for annual predictions made end Dec
clear;clc;

load P00Data;
load Market;

h = 1; NW = 1; gam = 5;
for ct = 1:2 % Simple/Log returns
    if ct==1
        RA = CRSPSP_A - Rf_A; RA = RA(12:12:end); RF = Rf_A(12:12:end); MktVar = 12*(MktStd(:,1).^2); MktVar = MktVar(12:12:end);
    else
        RA = log(1+CRSPSP_A) - log(1+Rf_A); RA = RA(12:12:end); RF = log(1+Rf_A(12:12:end)); MktVar = 12*(MktStd(:,2).^2); MktVar = MktVar(12:12:end);
    end
    load ResultsFile Results;
    for i = 1:46
        if strcmp(VARS(i).Freq,'Monthly')
            TBeg = find(yyyymm==Results(i).SampleBeg); TEnd = find(yyyymm==Results(i).SampleEnd); OOSBeg = find(yyyymm==Results(i).OOSBeg);
            if i==13 || i==17 % these are PLS variables
                XIS = VARS(i).DataIS{(ct-1)*4+3}; XOOS = VARS(i).DataOOS{(ct-1)*4+3};
            elseif i==22 % svix
                XIS = VARS(i).DataIS{3}; XOOS = VARS(i).DataOOS{3};
            else
                XIS = VARS(i).DataIS; XOOS = VARS(i).DataOOS;
            end
            XIS = XIS(12:12:end);
            XOOS = XOOS(12:12:end,12:12:end);
            TBeg = ceil(TBeg/12); TEnd = floor(TEnd/12); OOSBeg = ceil(OOSBeg/12);
            XIS = (XIS - mean(XIS(TBeg:TEnd),'omitnan'))/std(XIS(TBeg:TEnd),'omitnan');
        elseif strcmp(VARS(i).Freq,'Quarterly')
            TBeg = find(yyyyq==Results(i).SampleBeg); TEnd = find(yyyyq==Results(i).SampleEnd); OOSBeg = find(yyyyq==Results(i).OOSBeg);
            XIS =  VARS(i).DataIS; XIS = XIS(4:4:end);
            XOOS = VARS(i).DataOOS(4:4:end,4:4:end);
            TBeg = ceil(TBeg/4); TEnd = floor(TEnd/4); OOSBeg = ceil(OOSBeg/4);
            XIS = (XIS - mean(XIS(TBeg:TEnd),'omitnan'))/std(XIS(TBeg:TEnd),'omitnan');
        elseif strcmp(VARS(i).Freq,'Semiannual')
            TBeg = find(yyyys==Results(i).SampleBeg); TEnd = find(yyyys==Results(i).SampleEnd); OOSBeg = find(yyyys==Results(i).OOSBeg);
            XIS =  VARS(i).DataIS;  XIS = XIS(2:2:end);
            XOOS = VARS(i).DataOOS(2:2:end,2:2:end);
            TBeg = ceil(TBeg/2); TEnd = floor(TEnd/2);  OOSBeg = ceil(OOSBeg/2);
            XIS = (XIS - mean(XIS(TBeg:TEnd),'omitnan'))/std(XIS(TBeg:TEnd),'omitnan');
        elseif strcmp(VARS(i).Freq,'Annual')
            TBeg = find(yyyy==Results(i).SampleBeg); TEnd = find(yyyy==Results(i).SampleEnd); OOSBeg = find(yyyy==Results(i).OOSBeg);
            XIS =  VARS(i).DataIS;
            XOOS = VARS(i).DataOOS;
            XIS = (XIS - mean(XIS(TBeg:TEnd),'omitnan'))/std(XIS(TBeg:TEnd),'omitnan');
        end
        Results(i).SampleBeg = yyyy(TBeg); Results(i).SampleEnd = yyyy(TEnd); Results(i).OOSBeg = yyyy(OOSBeg); Results(i).OOSEnd = Results(i).SampleEnd;

        Results(i).ISStats = IS(XIS, RA, TBeg, TEnd, h, NW);

        [Aols,Aivx,Wivx,WivxInd,Q,corrmat] = ivxlh(RA(TBeg:TEnd), XIS(TBeg:TEnd), h, 0);
        Results(i).ISStats.KMSSlope = Aivx; Results(i).ISStats.KMSp = Wivx(2);

        Results(i).OOSStats = OOS(XOOS, RA, RF, MktVar, h, TBeg, TEnd, OOSBeg, gam);
    end % end loop i=1:47
    if ct==1
        Results_Simple_ADec = Results;
    else
        Results_Log_ADec = Results;
    end
end % end Simple/Log
save ResultsFile Results_* -append;
